﻿#pragma execution_character_set("utf-8")

#include "page_defaultDevList.h"
#include <QLayout>
#include <QLineEdit>
#include <QPushButton>
#include "devlist_view.h"
#include "devlist_data.h"

defaultDevListPage::defaultDevListPage(QWidget *parent) : QWidget(parent)
{
    this->buildTreeData();
    this->initForm();
}

defaultDevListPage::~defaultDevListPage()
{
}

void defaultDevListPage::initForm()
{
    QHBoxLayout *hLayout = new QHBoxLayout;
    this->seachEdit = new QLineEdit(this);
    this->seachEdit->setPlaceholderText(tr("请输入搜索内容"));
    connect(this->seachEdit, SIGNAL(returnPressed()), this, SLOT(OnSearch()));
    QPushButton* seachBtn = new QPushButton(this);
    connect(seachBtn, SIGNAL(clicked()), this, SLOT(OnSearch()));

    hLayout->setSpacing(1);
    hLayout->setContentsMargins(2, 0, 2, 0);
    hLayout->addWidget(seachEdit);
    hLayout->addWidget(seachBtn);

    QVBoxLayout *vLayout = new QVBoxLayout;
    this->devTree = new DevlistView(this);
    this->devTree->setProperty("noborder", "true");
    vLayout->setSpacing(10);
    vLayout->addLayout(hLayout);
    vLayout->addWidget(this->devTree);
    this->setLayout(vLayout);
    this->devTree->defaultExpand();
}

void defaultDevListPage::OnSearch()
{
    QString strSearch = this->seachEdit->text();
    this->devTree->showSearch(strSearch);
}

void defaultDevListPage::buildTreeData()
{
    GroupNode tmpGroup;
    DeviceNode tmpDevice;
    int i = 0;
    // 1
    tmpGroup.m_dwParentGroupID = 0;
    tmpGroup.m_dwGroupID = 1;
    strcpy(tmpGroup.m_strGroupName, "上海xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx公司");
    tmpGroup.m_bIsStore = false;
    GroupDeviceTree::Instance()->appendGroupNode(tmpGroup);

    // 2
    for (i = 0; i < 10; i++)
    {
        tmpGroup.m_dwParentGroupID = 1;
        tmpGroup.m_dwGroupID = 10+i;
        sprintf(tmpGroup.m_strGroupName, "层级%d", i+1);
        tmpGroup.m_bIsStore = false;    
        GroupDeviceTree::Instance()->appendGroupNode(tmpGroup);
    }

    // 3
    for (i = 0; i < 100; i++)
    {
        tmpGroup.m_dwParentGroupID = i%10 + 10;
        tmpGroup.m_dwGroupID = 100+i;
        sprintf(tmpGroup.m_strGroupName, "门店%d", i+1);
        tmpGroup.m_bIsStore = true;    
        GroupDeviceTree::Instance()->appendGroupNode(tmpGroup);
    }

    // 4
    for (i = 0; i < 1000; i++)
    {
        tmpDevice.m_dwStoreId = i%100 + 100;
        tmpDevice.m_bOnline = i%10==0 ? true:false;
        sprintf(tmpDevice.m_strDeviceSn, "%d", i+1);
        sprintf(tmpDevice.m_strShowName, "设备%d", i+1);
        GroupDeviceTree::Instance()->appendDeviceNode(tmpDevice);
    }
}
